package com.ehome.poct.core.controller;

import com.ehome.core.page.PageInfo;
import com.ehome.poct.core.api.dto.CheckStripQry;
import com.ehome.poct.core.api.dto.check.TestStripPriceUpdateDTO;
import com.ehome.poct.core.api.dto.check.gold.TestStripPriceVO;
import com.ehome.poct.core.api.dto.check.project.EhomeProjectDTO;
import com.ehome.poct.core.api.mapping.CheckDataMapping;
import com.ehome.poct.core.api.virtual.CheckProjectService;
import com.ehome.poct.core.api.virtual.CheckTypeInfoService;
import com.ehome.poct.core.api.vo.check.PoctTypeVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "检测信息")
@ApiSupport(author = "cq")
@RestController
@RequestMapping("/jcstripinfo")
@AllArgsConstructor
public class JcStripInfoController {
    @Autowired
    CheckProjectService checkProjectService;
    @Autowired
    CheckDataMapping mapping;
    @Autowired
    CheckTypeInfoService checkTypeInfoService;
    /**
     *
     * 金标仪检测类型
     */
    @ApiOperation("金标仪拥有的试纸类型")
    @GetMapping("/checkInfo/{type}")
    @ApiOperationSupport(author = "cq")
    public List<PoctTypeVO> jbyCheckInfo(@PathVariable @ApiParam("0：所有检测类型；1：金标仪检测类型；2：血常规检测类型；3：联机检测类型") Integer type)
    {
        List<EhomeProjectDTO>  dto= checkProjectService.selectWithType(type);
        return mapping.toPoctTypes(dto);
    }
    /**
     *
     * 金标仪检测类型 原价
     */
    @ApiOperation("检测原价列表")
    @PostMapping("/checkprice")
    @ApiOperationSupport(author = "cq")
    public PageInfo<TestStripPriceVO> getList(@RequestBody CheckStripQry qry){

        List<EhomeProjectDTO>  dto= checkProjectService.selectWithType(qry);
        if(qry.getCheckType()!=null){
            dto = dto.stream().filter(t->t.getCode().equals(qry.getCheckType())).collect(Collectors.toList());
        }
        if(qry.getStartTime()!=null){
            dto = dto.stream().filter(t->t.getUpdateTime().isAfter(qry.getStartTime())).collect(Collectors.toList());
        }

        if(qry.getEndTime()!=null){
            dto = dto.stream().filter(t->t.getUpdateTime().isBefore(qry.getEndTime())).collect(Collectors.toList());
        }
        if(qry.getShowOriginPrice()!=null){
            dto = dto.stream().filter(t->t.getShowOriginPrice().equals(qry.getShowOriginPrice())).collect(Collectors.toList());
        }
        //总共的数据未来几年都不会超过50条，直接内存分页
        List<TestStripPriceVO> datas = mapping.toStripPrices( dto.stream()
                .skip((qry.getPageIndex() - 1) * Long.valueOf(qry.getPageSize()))
                .limit(qry.getPageSize()).collect(Collectors.toList()));
        PageInfo<TestStripPriceVO> pages = new PageInfo<>();
        pages.setList(datas);
        pages.setTotal(dto.size());
        pages.setCurrent(qry.getPageIndex());
        pages.setCurrent(qry.getPageSize());
        return pages;
    }
    /**
     *
     * 金标仪检测类型 原价
     */
    @ApiOperation("启用/禁用 原价显示")
    @PutMapping("/disable/{conftypeid}")
    @ApiOperationSupport(author = "cq")
    public boolean disablePrice(@PathVariable Long conftypeid)
    {
        var stripType= checkTypeInfoService.getById(conftypeid);
        if(null!=stripType)
        {
            stripType.setShowOriginPrice(stripType.getShowOriginPrice().equals(0)?1:0);
            stripType.setUpdateTime(LocalDateTime.now());
            return null!=checkTypeInfoService.update(conftypeid,stripType);
        }else{
            var bloodStripType =  checkProjectService.getById(conftypeid);
            if(null!=bloodStripType){
                bloodStripType.setShowOriginPrice(bloodStripType.getShowOriginPrice().equals(0)?1:0);
                return null!=checkProjectService.update(conftypeid,bloodStripType);
            }
        }
    return false;
    }
    /**
     *
     * 修改金标仪检测类型 原价
     */
    @ApiOperation("修改价格")
    @PostMapping("/modify")
    @ApiOperationSupport(author = "cq")
    public boolean updatePrice(@RequestBody TestStripPriceUpdateDTO vo)
    {
        var stripType= checkTypeInfoService.getById(vo.getTestConfPoctStripTypeId());
        if(null!=stripType) {
            stripType.setPrice(vo.getPrice());
            stripType.setUpdateTime(LocalDateTime.now());
            return null!=checkTypeInfoService.update(vo.getTestConfPoctStripTypeId(),stripType);
        }else
        {
            var bloodStripType =  checkProjectService.getById(vo.getTestConfPoctStripTypeId());
            if(null!=bloodStripType){
                bloodStripType.setSalePrice(vo.getPrice());
                return null!=checkProjectService.update(vo.getTestConfPoctStripTypeId(),bloodStripType);
            }
        }
        return false;
    }


}
